// page/diary-write/index.js
const config = require('../../config')
const common = require('../../util/common');
const db = wx.cloud.database();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    btnCan: false,
    loadingHidden: true,
    canEdit: false,
    showLeft: false,
    showRight: false,
    length: 0,
    diary: "",
    post_data: {
      diaryid: "",
      weather: "-1",
      mood: "-1",
      content: ""
    }
  },

  handlerGobackClick() {
    const pages = getCurrentPages();
    if (pages.length >= 2) {
      wx.navigateBack({
        delta: 1
      });
    } else {
      this.jump2DiaryList()
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 拿数据
    var that = this;
    wx.getStorage({
      key: 'diary',
      success: function (res) {
        // console.log(res.data);
        var len = res.data.content.length;
        that.setData({
          diary: res.data,
          length: len
        });
        that.data.post_data.content = res.data.content;
        that.data.post_data.diaryid = res.data._id;

        // 判断日记是否可以编辑
        if (new Date(res.data.createDate).getTime() == new Date(new Date().toLocaleDateString()).getTime()) {
          that.setData({
            canEdit: true
          })
        } else {
          that.setData({
            canEdit: false
          })
        }
      },
    });

  },

  onShow: function() {
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 统计字数
  countNum: function (e) {
    // console.log(e.detail.value.length);
    var num = e.detail.value.length;
    this.setData({
      length: num,
    });
    this.data.post_data.content = e.detail.value;
  },

  update: async function () {
    var _this = this;
    var content = _this.data.post_data.content;
    var mood = _this.data.post_data.mood;
    var weather = _this.data.post_data.weather;
    var diaryid = _this.data.post_data.diaryid;
    _this.setData({
      btnCan: true,
      loadingHidden: false
    })
    wx.cloud.callFunction({
      name: 'contentCheck',
      data: {
        msg: content
      },
      success: function(res) {
        db.collection('diary').doc(diaryid).update({
          data: {        
            weather: weather,      
            mood: mood,
            week: _this.data.today_week,
            content: content,
            createDateStr: _this.data.createDateStr,
            createTime: db.serverDate()
          },
          success: function(res){
            wx.showToast({
              title: '修改成功',
              icon: 'success',
              duration: 1000
            });
            wx.setStorage({
              key: 'refresh',
              data: true,
            })        
            setTimeout(function(){
              // 返回到上一页
              _this.jump2DiaryList()
            },700);
            
            
          }      
        })
      },
      fail: function(err) {
        wx.showToast({
          title: '含敏感内容',
          icon: 'error',
          duration: 1500
        });
      },
      complete: function() {
        _this.setData({
          btnCan: false,
          loadingHidden: true
        })
      }
    })
    // var isPassCheck = await common.contentCheck(content)
    // if (isPassCheck) {      
    //   db.collection('diary').doc(diaryid).update({
    //     data: {        
    //       weather: weather,      
    //       mood: mood,
    //       week: _this.data.today_week,
    //       content: content,
    //       createDateStr: _this.data.createDateStr,
    //       createTime: db.serverDate()
    //     },
    //     success: function(res){
    //       wx.showToast({
    //         title: '修改成功',
    //         icon: 'success',
    //         duration: 1000
    //       });
    //       wx.setStorage({
    //         key: 'refresh',
    //         data: true,
    //       })        
    //       setTimeout(function(){
    //         // 返回到上一页
    //         _this.jump2DiaryList()
    //       },700);
    //     }      
    //   })
    // }else {
    //   wx.showToast({
    //     title: '含敏感内容',
    //     icon: 'error',
    //     duration: 1500
    //   });
    // }
  },
  /**
   * 删除日记
   */
  deleteDiary: function () {
    let _this = this;
    wx.showModal({
      title: '提示',
      content: '删除后将不能恢复，您确认删除吗？',
      cancelText: '取消',
      confirmText: '确定',
      success: function (res) {
        if (res.confirm) {
          let diaryid = _this.data.post_data.diaryid;
          // console.log('用户点击确定')
          db.collection('diary').doc(diaryid).remove({            
            success: function(res){
              wx.showToast({
                title: '删除成功',
                icon: 'success',
                duration: 1000
              });
              wx.setStorage({
                key: 'refresh',
                data: 'true',
              })
              setTimeout(function(){
                // 返回到上一页
                _this.jump2DiaryList()
              },1000);             
            }      
          })
        } else if (res.cancel) {
          // console.log('用户点击取消')

        }
      }
    })
  },


  jump2DiaryList() {
    wx.switchTab({
      url: '/page/diary-list/index'
    });
  }

})